package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ymm
 * @Description: 类内容说明
 * @Date: Created in 18:56 2021/1/11
 * @ModifiedBy:
 */
@Service
@Slf4j
public class QuanziMQService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    /**
     * 发布动态消息
     * @param publishId
     * @return
     */
    public Boolean publishMsg(String publishId){
        return this.sendMsg(publishId,1);
    }

    /**
     * 浏览动态消息
     * @param publishId
     * @return
     */
    public Boolean queryPublishMsg(String publishId){
        return this.sendMsg(publishId, 2);
    }

    /**
     * 点赞动态消息
     * @param publishId
     * @return
     */
    public Boolean likePublishMsg(String publishId){
        return this.sendMsg(publishId, 3);
    }

    /**
     * 取消点赞动态消息
     * @param publishId
     * @return
     */
    public Boolean disLikePublishMsg(String publishId){
        return this.sendMsg(publishId, 6);
    }

    /***
     * 喜欢动态消息
     * @param publishId
     * @return
     */
    public Boolean lovePublishMsg(String publishId){
        return this.sendMsg(publishId, 4);
    }

    /**
     * 取消喜欢动态消息
     * @param publishId
     * @return
     */
    public Boolean disLovePublishMsg(String publishId){
        return this.sendMsg(publishId, 7);
    }


    /**
     * 评论动态消息
     * @param publishId
     * @return
     */
    public Boolean commentPublishMsg(String publishId){
        return this.sendMsg(publishId, 5);
    }

    /**
     * 发送圈子操作相关的消息
     * @param publishId
     * @param type
     * 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
     * @return
     */
    private Boolean sendMsg(String publishId, Integer type) {
        try {
            //1.获取当前用户
            User user = UserThreadLocal.get();

            //2.调用quanZiApi进行查询发布人
           /* Publish publish = this.quanZiApi.queryPublishById(publishId);*/
           //可以利用缓存中的数据进行查询信息
            Object pid = redisTemplate.opsForHash().get("TANHUA_ID_HASH_PUBLISH", publishId);

            //构建消息
            Map<String,Object> msg = new HashMap<>();
            msg.put("userId",user.getId());
            msg.put("date",System.currentTimeMillis());
            msg.put("publishId",publishId);
            msg.put("pid",pid);
            msg.put("type",type);

            //发送信息到推荐系统
            this.rocketMQTemplate.convertAndSend("tanhua-quanzi",msg);
        } catch (MessagingException e) {
            log.error("发送消息失败! publishId = " + publishId + ", type = " + type, e);
            return false;
        }
        return true;
    }
}
